<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #cs {
        border: 1px solid #999;
      }
    </style>
  </head>
  <body>
    <canvas id="cs" width="400px" height="400px">您的浏览器不支持canvas</canvas>
  </body>
  <script>
    var cs = document.getElementById("cs");
    if (cs.getContext("2d")) {
      var ctx = cs.getContext("2d");
      ctx.fillStyle = "pink";
      ctx.fillRect(50, 50, 150, 150);
      ctx.save(); //设置还原点1
      ctx.fillStyle = "purple";
      ctx.fillRect(100, 100, 150, 150);
      ctx.save(); //设置还原点2
      ctx.fillStyle = "yellow";
      ctx.fillRect(150, 150, 150, 150);
      ctx.restore(); //恢复到还原点2
      ctx.fillRect(200, 200, 150, 150);
      ctx.restore(); //恢复到还原点1
      ctx.fillRect(250, 250, 150, 150);
    } else {
      console.log("您的浏览器不支持canvas");
    }
  </script>
</html>
